<template>
  <ul class="list">
    <li
      class="item"
      v-for="item of cities"
      :key="item"
      :ref="item"
      @click="handleLetterChange"
      @touchstart.prevent="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="handleTouchEnd"
    >
      {{item}}
    </li>
  </ul>
</template>

<script>
export default {
  name: 'CityAlphabet',
  props: {
    cities: Array
  },
  data(){
    return {
      touchStatus:false,
      startY:"",
      timer:null
    }
  },
  methods:{
    handleLetterChange(e){
      this.$emit('change',e.target.innerText)
    },
    handleTouchStart(){
      this.touchStatus=true;
    },
    handleTouchMove(e){
      if (this.touchStatus) {
          if (this.timer) {
            clearTimeout(this.timer);
          }
          this.tiemr=setTimeout(()=>{
            const startY=this.$refs['A'][0].offsetTop;
            const touchY=e.touches[0].clientY-156;
            const index=Math.floor((touchY-startY)/50);
            if (index>=0 && index<=this.cities.length) {
                this.$emit('change',this.cities[index])
            }
          },16);
      }
    },
    handleTouchEnd(){
      this.touchStatus=false;
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import '../../../assets/styles/varibles.styl'
  .list
    display: flex
    flex-direction: column
    justify-content: center
    position: absolute
    top: 1.58rem
    right: 0
    bottom: 0
    width: .4rem
    z-index:3
    .item
      line-height: .5rem
      text-align: center
      color: $bgColor
</style>
